import {Component, Inject, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {DeliveryModule} from '../../../service/delivent/deliveryModule';
import {UtilityService} from '../../../service/utils.service';
import {appConfig} from '../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';
import {ResponseContentType} from '@angular/http';
import * as moment from 'moment';
import * as _ from 'lodash';

@Component({
    selector: 'app-launch-apply',
    templateUrl: './launch-apply.component.html',
    styleUrls: ['./launch-apply.component.less'],
})
export class LaunchApplyComponent implements OnInit {
    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        private confirmServ: NzModalService,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    ) { }
    // 界面变量
    loadingnext = false;
    showAdd: boolean; // 是否有修改
    isStatus = true;
    configTitle = '详情'
    modalVisible = false;
    checkModalVisible = false;
    mergeVisible = false; // 合并投放弹窗
    checkListVisible = false; // 核查清单弹出
    isPagination = true; // 是否有分页
    istextVisible = false;
    isMergeList = false; // 合并列表模态框
    checkloading = true;
    // 信息
    search: DeliveryModule = new  DeliveryModule();
    deliveryTime: any; // 投放日期

    pageTotal: number; // 翻页总数
    checkStatus = true;
    copyseniorGuid: string;
    isShowTotalhead = true;
    token: any;
    userName: string;
    isNext = true;
    initDate: any;
    elementScice: any; // 环境数据
    elementCopy: any; // copy环境接口
    expandForm = true;
    data: any[] = []; // 表格数据
    mergeList: any[] = [];
    mergeListInfo: any[] = [];
    profiles: any[] = [];
    updEnvironment = false;
    isloading = true;
    // 表头
    headerDate = [  // 配置表头内容
        { label: '别名', value: 'applyAlias', isclick: false, radio: false},
        { label: '工作项', value: 'guidWorkitem', isclick: false, radio: false },
        { label: '提交申请时间', value: 'applyTime', isclick: false, radio: false },
        { label: '申请投放日期', value: 'deliveryTime', isclick: false, radio: false },
        { label: '运行环境', value: 'guidProfiles', isclick: false, radio: false },
        { label: '打包窗口',value: 'packTiming', isclick: false, radio: false },
        { label: '申请人', value: 'proposer', isclick: false, radio: false },
        { label: '投放结果', value: 'deliveryResult', isclick: false, radio: false },
    ];
    // 配置表头内容
    mergeHeader = [

        { label: '工作项', value: 'guidWorkitem', isclick: false, radio: false },
        { label: '分支', value: 'branch', isclick: false, radio: false },
        { label: '版本范围',value: 'branchRange', isclick: false, radio: false },
        { label: '申请人', value: 'proposer', isclick: false, radio: false },
        { label: '投放结果', value: 'deliveryResult', isclick: false, radio: false },
    ];

    // 传入按钮层
    moreData = {
        morebutton: true,
        buttonData: [
            { key: 'Overview', value: '查看概况' }
        ]
    }

    buttonData = [
        {key: 'details', value: '详情'},
        // {check: false, value: '未确认合并'},
    ];
    test: string;
    page: any;
    total: number;
    pages: number;
    branches: any[] = []; // 合并清单分支数组
    patchCount: any[] = []; // 投放小计
    detailList: any[] = []; // 合并清单代码数组
    profilesData: any;
    checkModalData: any[] = []; // 核查清单数据
    mergeListData: any[] = []; // 核查有异议的数据
    isShowDate = false;
    detailVisible = false;
    copyVisible = false;
    launchVisible = false;
    detailInfo: any; // 投放之后的详情
    copyTitle: string;
    inputValue = '';
    mergeListDetail: any[] = []; // 投放申请详情
    pageIndex = 1;
    checkId: any;
    current = 0;
    loading: any;
    index = 'First-content';
    updPackTiming = {}
    guidDelivery: any;
    guid: any;
    deliveryResults = appConfig.Enumeration.deliveryResults
    buttons = [
        // {key: 'merge', value: '合并投放', if: true},
        {key: 'checking', value: '去核对',  if: false},
        {key: 'export', value: '导出投放清单',  if: true},
    ]

    role:string;
    revertListData = [];

    revertListHeaderDate = [
        { label: '代码', value: 'projectCode', isclick: false, radio: false  },
        { label: '合并类型', value: 'fileUpdateType',  isclick: false, radio: false },
        { label: '提示', value: 'conflictType',  isclick: false, radio: false },
    ]
    revertPageIndex = 1;
    childListData = [];
  childListHeaderDate =[
      { label: '工作项', value:'workitem', isclick: false, title: true },
      { label: '代码', value:'fullPath', isclick: false, title: true },      
      { label: '操作人', value:'owner', isclick: false, title: true },
      { label: '时间', value: 'deliveryTime', isclick: false, title: true }, 
      
  ]
  childListDataTotal:number;
    // 初始化
    ngOnInit() {
        this.token  = this.tokenService.get().seniortoken;
        this.userName = this.tokenService.get().name;
        this.role = this.tokenService.get().role;
        this.getElement();
        this.getData();
        this.showAdd = true;
    }

listloading = false;
    getData(type?) {
        if (type === 'search') {
            this.pageIndex = 1;
        }
        const page = {
            condition: this.search,
            page : {
                size: 10,
                current :  this.pageIndex,
                orderByField: 'guid',
                asc: false // asc 默认是true  升序排序，时间类型 用false， 降序
            }
        };



        this.listloading = true;
        this.utilityService.postData(appConfig.testUrl  + appConfig.API.list, page)
            .subscribe(
                (val) => {
                     this.listloading = false;
                    if (val.code === '200') {
                        this.data = val.result.records;
                        this.total = val.result.total; // 总数
                        this.pageTotal = val.result.pages * 10; // 页码

                        for ( let i = 0; i < this.data.length; i++) {
                            this.data[i].deliveryTime = moment(this.data[i].deliveryTime).format('YYYY-MM-DD');
                            this.data[i].applyTime = moment(this.data[i].applyTime).format('YYYY-MM-DD HH:mm:ss');
                            this.data[i].profilesGuid = this.data[i].guidProfiles.source;
                            this.data[i].guidProfiles = this.data[i].guidProfiles.target;
                            this.data[i].workGuid = this.data[i].guidWorkitem.source;
                            this.data[i].guidWorkitem = this.data[i].guidWorkitem.target;
                            this.deliveryResults.forEach(j=>{
                               if(this.data[i].deliveryResult === j.value){

                                  this.data[i].color = j.color
                                this.data[i].icon = j.icon
                               }
                            })
                            if (this.data[i].deliveryResult === '申请中') {

                                if(this.role === "RCT小组") {
                                     this.data[i].buttonData = [
                                        {key: 'dels', value: '删除' },
                                        {key: 'detail', value: '详情'},
                                        {key: 'upd', value: '调窗口'},
                                        {key: 'copy', value: '投放新环境'},
                                        {key: 'refuse', value: 'RCT拒绝'},
                                        {key: 'test', value: '测试合并'}
                                    ]
                                }else {
                                    this.data[i].buttonData = [
                                        {key: 'dels', value: '删除' },
                                        {key: 'detail', value: '详情'},
                                        {key: 'upd', value: '调窗口'},
                                        {key: 'copy', value: '投放新环境'},
                                        {key: 'test', value: '测试合并'}


                                    ]
                                }
                            }else {
                                this.data[i].buttonData = [{key: 'detail', value: '详情'}, {key: 'marigeList', value: '合并报告'}];
                            }

                        }
                    }
                    console.log(this.data)


                },
                (error) => {
                       this.listloading = false;
                   this.nznot.create('error', error.code,  error.msg);
                }
            );

    }

    // 重置
    reset() {
        this.search = new  DeliveryModule();
        this.getData();
    }

    // 请求环境接口
    getElement() {
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sProfiles)
            .subscribe(
                (val) => {
                    this.elementScice = val.result;
                    for (let i = 0; i < this.elementScice.length; i++) {
                        this.elementScice[i].packTiming = this.elementScice[i].packTiming.split(',');
                        this.elementScice[i].Timing  = []
                        for ( let s = 0; s < this.elementScice[i].packTiming.length; s ++) {
                            let obj = {
                                time: this.elementScice[i].packTiming[s]
                            };
                            this.elementScice[i].Timing.push(obj);
                        }
                    }
                    // 拼接
                }, (error) => {
                  this.nznot.create('error', error.code,  error.msg);
                }
            );
    }


    // 调用投放环境接口
    getcheckOptionOne(guid) {
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.copyProfiless + '/' + guid + '/delivered')
            .subscribe(
                (val) => {
                    this.elementCopy = val.result;
                    for (let i = 0; i < this.elementCopy.length; i++) {
                        this.elementCopy[i].deliveryName = '第一次投放'
                        this.elementCopy[i].deliveryTime = new Date(this.elementCopy[i].deliveryTime); // 初始化时间
                        this.elementCopy[i].unixTime = moment(this.elementCopy[i].deliveryTime).format('YYYY-MM-DD 00:00:00.000')
                        if (this.elementCopy[i].delivered) { // 如果为true，那么checkbox即radio全部禁选
                            this.elementCopy[i].disabled = true;
                        }
                        for (let s = 0; s < this.elementCopy[i].packTimeDetails.length; s ++) {
                            if (this.elementCopy[i].packTimeDetails[s].isOptions === 'D') {
                                this.elementCopy[i].times = this.elementCopy[i].packTimeDetails[s].packTime;
                            }
                        }
                    }
                }
            );
    }

    // 列表组件传过来的内容
    addHandler(event) {
        if (event === 'merge') {
            this.mergeListInfo = [];
            this.mergeList = [];
            for ( let i = 0; i < this.data.length; i++) {
                // 清空数组
                if (this.data[i].checked === true) {
                    this.mergeListInfo.push(this.data[i]);
                }
            }
            if (this.mergeListInfo.length === 0) {
                this.nznot.create('error', '请检查是否勾选工程', '请检查是否勾选工程');
                return;
            }
            for ( let i = 0; i < this.mergeListInfo.length; i++) {
                if (this.mergeListInfo[0].guidProfiles !== this.mergeListInfo[i].guidProfiles){
                    this.nznot.create('error', '合并项目运行环境必须一致', '合并项目运行环境必须一致');
                    return;
                }
                if (this.mergeListInfo[i].deliveryResult !== '成功') {
                    this.nznot.create('error', '只能合并投放结果为成功的项目', '只能合并投放结果为成功的项目');
                    return;
                }
                this.mergeList.push(this.mergeListInfo[i].guid);
            }
            this.mergeisVisible = true;
        } else if (event === 'checking') {
            this.loadingnext = false
            this.getSprofiles();
            this.mergeisVisible = true
            this.current = 0;
        } else if (event === 'export') {
            this.deliveryTime = moment(new Date()).format('YYYY-MM-DD');
               this.getSprofiles(); // 查询环境和时间
               console.log(this.elementScice)
            this.importCurrent = 0;
               this.indeterminate = false;
               this.allChecked = false;
            this.workItem = false;
              this.isVisible = true;
        }
    }

    // 列表传入的翻页数据
    monitorHandler(event) {
        this.pageIndex = event;
       this.getData();
    }

    // 接受子组件删除的数据 单条还是多条
    deleatData(event) {

    }

    getSprofiles() {
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sProfiles )
            .subscribe(
                (val) => {
                    this.elementScice = val.result;
                    for (let i = 0; i < this.elementScice.length; i++) {
                        this.elementScice[i].packTiming = this.elementScice[i].packTiming.split(',');
                        this.elementScice[i].Timing  = []
                        for ( let s = 0; s < this.elementScice[i].packTiming.length; s ++) {
                            let obj = {
                                time: this.elementScice[i].packTiming[s]
                            };
                            this.elementScice[i].Timing.push(obj);
                        }
                    }
                    // 拼接

                }, (error) => {
                   this.nznot.create('error', error.code,  error.msg);
                }
            );
    }

   isdiabled = true;
    pre(): void {
        this.current -= 1;
        this.changeContent();
    }
initOptions =[];
isWorkitem = {
    'disable':false,
    'title':'请选择工作项及工程名'
}
    next(): void {
        this.loadingnext = true;
        this.isdiabled = false;
        let profiles = '';
        let packTiming = '';
        for (let i = 0; i < this.elementScice.length; i ++) {
            if (this.elementScice[i].check && this.elementScice[i].times) {
                profiles = this.elementScice[i].guid;
                packTiming = this.elementScice[i].times;

            }
        }

        let url = '';
        if (profiles !== '' && packTiming !== '') {
            url = appConfig.testUrl + '/checks/profiles/' + profiles + '/packTiming/' + packTiming;
        }else {
            this.nznot.create('error', '请选择运行环境！', '请选择运行环境！');
            this.loadingnext = false;
            return;
        }

        if(this.current == 0) {
            let index = '';
            let indexs = '';
            //   this.current += 1;
            let arr = [];
            this.loading = true;

            // 跳转核对列表
            this.utilityService.getData( url )
                .subscribe(
                    (val) => {
                        this.loading = false;
                        this.loadingnext = false;
                           this.isdiabled = true;
                        if (val.code === '200') {

                            this.current += 1;

                            for (let i = 0; i < val.result.length; i++) {
                                arr[i] = val.result[i].delivery
                                arr[i]['branch'] = val.result[i].branch
                                arr[i]['projectList'] = val.result[i].projectList;
                                arr[i]['expand'] = false;
                                arr[i]['check'] = true;
                                arr[i]['deliveryTime'] = moment(val.result[i].deliveryTime).format('YYYY-MM-DD');
                                arr[i]['branchRange']=val.result[i].sRevision+'-'+val.result[i].eRevision
                                if (val.result[i].delivery.deliveryResult === '申请中') {
                                    arr[i]['buttonData']= [ {check: false, value: '确认合并', key:'yes',isLoadingOne:false}];
                                }else{
                                    // arr[i]['buttonData']= [ {check: false, value: '取消合并', key:'no'}];
                                    arr[i]['buttonData']= [ {check: false, value: '已合并', key: 'no',isLoadingOne:false}, {check: false, value: '查看报告', key: 'lookList',isLoadingOne:false }];
                                }
                                arr[i]['guidWorkitem'] = val.result[i].delivery.guidWorkitem.target;
                            }

                            this.initDate = arr;


                        }else {
                            this.isdiabled = true;
                            this.nznot.create('error', val.msg, val.msg);

                        }
                    },
                    (error) => {

                        this.loadingnext = false;
                        this.isdiabled = true;
                       this.nznot.create('error', error.code,  error.msg);

                    })

            // step2
        }else if (this.current === 1){

                let index = '';
                let indexs = '';
                this.initOptions = []
                this.isNow = false;

this.utilityService.postData(url, {},  )
                    .subscribe(
                        (val) => {
                            console.log(val)
                                this.isdiabled = true;
                                this.loadingnext = false;
                                this.checkId = val.result.check.guid;
                                this.checkloading = false;
                                this.current += 1;
                                this.checkListVisible = true;
                                this.checkStatus = true;
                                let icon= ''
                                let color =''
                                let disabledS:boolean
                   if(val.result.deliveryDetails.length == 0){
                               this.isWorkitem.disable = true;
                               this.isWorkitem.title = '暂无数据'
                               let obj = {
                                    'value':'',
                                    'label':'暂无数据',
                                    'disable':true
                                }
                              this.initOptions.push(obj)
                  }else{

                            this.isWorkitem.disable = false;
                               this.isWorkitem.title = '请选择工作项及工程名'
                    for(let i =0;i<val.result.deliveryDetails.length;i++){
                            this.deliveryResults.forEach(x=>{
                                if(x.value == val.result.deliveryDetails[i].delivery.deliveryResult){
                                    color = x.color;
                                    icon = x.icon
                                }
                            })

                            if (  val.result.deliveryDetails[i].delivery.deliveryResult === '核对中' ){
                                        disabledS = false;
                                        }else {
                                        disabledS= true;
                                    }
                            let obj = {
                                'value':val.result.deliveryDetails[i].delivery.guid,
                                'label':val.result.deliveryDetails[i].delivery.guidWorkitem.target,
                                'deliveryResult':val.result.deliveryDetails[i].delivery.deliveryResult,
                                'deliveryTime':val.result.deliveryDetails[i].delivery.deliveryTime,
                                'profiles':val.result.deliveryDetails[i].delivery.guidProfiles.target,
                                'proposer':val.result.deliveryDetails[i].delivery.proposer,
                                'disabledS':'',
                                'checkingloading':false,
                                'icon':icon,
                                'color':color,
                                'disable':false
                            }
                            this.initOptions.push(obj)

                            }
                     }
                      this.getMergeList();
                        } ,
                (error) => {
                       this.loadingnext = false;
                          this.isdiabled = true;
                   this.nznot.create('error', error.code,  error.msg);
                })




            // }


        }



    }

    done(): void {

    }
    YpageIndex = 1;
    YpageTotal:number;
     getMergeList(){
      const page = {
            page : {
                size: 10,
                current : this.YpageIndex,
                asc: false
            }
        };

                let url = '/checks/'+this.checkId+'/checkList/merge/list'
                   let index = '';
              let indexs = '';
          this.utilityService.postData(appConfig.testUrl  + url, page)
                    .subscribe(menu => {
                        this.YpageTotal = menu.result.total;
                         this.mergeListData  = menu.result.records;
                          let star = '';
                          let end = '';
                        for  (let i = 0; i < this.mergeListData.length; i ++) {

                             switch(this.mergeListData[i].confirmStatus){
                                case '加入投放':
                                 this.mergeListData[i]['checkbuttons'] = true;
                                 break;
                                  case '删除':
                                 this.mergeListData[i]['checkbuttons'] = false;
                                 break;
                                  default :
                                 this.mergeListData[i]['checkbuttons'] = null;
                                 break;
                            }
                            if (this.mergeListData[i].fullPath) {
                                indexs = this.mergeListData[i].fullPath.indexOf(this.mergeListData[i].partOfProject);
                                this.mergeListData[i].fullPath = this.mergeListData[i].fullPath.substring(indexs, this.mergeListData[i].fullPath.length);
                                                  if(this.mergeListData[i].fullPath.length > 30){
                                                        star = this.mergeListData[i].fullPath.substr(0, 10)
                                                        end = this.mergeListData[i].fullPath.substr(this.mergeListData[i].fullPath.length - 20)
                                                           this.mergeListData[i].fullPathstr = star + '...' + end;
                                                        }else {
                                                            this.mergeListData[i].fullPathstr = this.mergeListData[i].fullPath;
                                                     }
                         }
                           if (this.mergeListData[i].programName.length > 40){
                                                        star = this.mergeListData[i].programName.substr(0, 10)
                                                        end = this.mergeListData[i].programName.substr(this.mergeListData[i].programName.length - 10)
                                                           this.mergeListData[i].programNamestr = star + '...' + end;
                                                        }else{
                                                            this.mergeListData[i].programNamestr = this.mergeListData[i].programName;
                                                     }
                        }


                    }
                     ,
                (error) => {

                   this.nznot.create('error', error.code,  error.msg);
                })
    }
   DopageNumber(event){
           console.log(event)
       this.YpageIndex = event
       this.getMergeList();

   }
    changeContent(): void {
        switch (this.current) {
            case 0: {
                this.getSprofiles();
                break;
            }
            case 1: {
                this.index = '<h2>顶顶顶顶</h1>';
                break;
            }
            case 2: {
                this.index = '<h3>顶顶顶顶</h1>';
                break;
            }
            default: {
                this.index = 'error';
            }
        }
    }

    hide() {

    }
    chancanle(){
        this.iStouchan = false;
    }

    buttonEventMerge(event) {

        let url = '';
        let value = '';
        let key = '';
        let str = '';
       event.event.isLoadingOne = true;
        if(event.event.key === 'yes'){
            url = appConfig.testUrl + '/deliveries/' + event.guid.guid + '/real';
             value = '取消合并'
             key = 'no'
             str = '已合并'
            // 请求地址
             console.log(event)
            this.utilityService.getData( url, {},  )
                .subscribe(
                    (val) => {
                        this.isdiabled = true;
                      event.event.isLoadingOne = false;
                        if (val.code === '200') {
                           if(val.result.realMerge === false) {
                               event.event.value = '确认合并';
                               event.guid.deliveryResult = '申请中';
                               event.event.key = 'yes';
                               this.nznot.create('error', val.msg, val.msg);
                           } else {
                               event.event.value = '已合并'
                               event.guid.deliveryResult = str
                               event.event.key = 'no';
                               this.nznot.create('success', val.msg, val.msg);
                               
                               event.guid.buttonData.push({check: false, value: '查看报告', key: 'lookList',isLoadingOne:false })
                           }
                            this.isTestModal = true;
                            this.errorInfo =  val.result;
                            this.revertListData = val.result.conflictReportDetails;
                               for(let i=0;i<this.revertListData.length;i++){
                           if(this.revertListData[i].whoChangedConflictDetails.length>0){
                               this.revertListData[i].buttonData = [
                                   {key: 'WhoChange', value: '查看谁也再改' }
                               ]
                           }
                         
                       }
                        }else {
                            this.nznot.create('error', val.msg, '');
                        }

                    }
                    ,
                    (error) => {
                         event.event.isLoadingOne = false;
                        this.isdiabled = true;
                        this.nznot.create('error', error.code,  error.msg);
                    }
                );

        } else if (event.event.key === 'lookList') {
            console.log(event)
         
            this.utilityService.getData(appConfig.testUrl  + '/deliveries/' + event.guid.guid + '/mergeReport')
                .subscribe(
                    (val) => {
                         event.event.isLoadingOne = false;
                        this.isTestModal = true;
                        this.errorInfo =  val.result;
                        this.revertListData = val.result.conflictReportDetails; // 显示弹窗
                             for(let i=0;i<this.revertListData.length;i++){
                           if(this.revertListData[i].whoChangedConflictDetails.length>0){
                               this.revertListData[i].buttonData = [
                                   {key: 'WhoChange', value: '查看谁也再改' }
                               ]
                           }
                       }
                        this.nznot.create('success', val.msg, val.msg);
                    }   ,
                    (error) => {
                         event.event.isLoadingOne = false;
                        this.nznot.create('error', error.msg, '');
                    }
                );
        } else if(event.event.key === 'no') {
              event.event.isLoadingOne = false;
        url = appConfig.testUrl + '/deliveries/' + event.guid.guid + '/unmerge';
            value = '确认合并'
            key = 'yes'
               str = '申请中'
        }
    }

    // 按钮点击事件
    packTime:any;
    errorInfo = {};  // 详情
    buttonEventlist(event) {
        console.log(event)
        if (event.names.key === 'dels') {
            let self = this;
            this.confirmServ.confirm({
                title  : '提示',
                content:'<b>您是否确认要删除同一批次工作项[ '+event.itemName+' ]的所有申请!</b>',
                showConfirmLoading: true,
                onOk() {
                    self.utilityService.deleatData(appConfig.testUrl  + appConfig.API.deliveries + '/' + event.guid)
                        .subscribe(
                            (val) => {
                                if (val.code === '200') {
                                    if ( !(( self.total - 1) % 10)) {
                                        self.pageTotal = self.pageTotal - 10 ;
                                        // self.getData();
                                    }
                                    self.getData();

                                    self.nznot.create('success', val.msg, val.msg);
                                }else {
                                    self.nznot.create('error', val.msg, '');
                                }
                            }   ,
                            (error) => {
                                self.nznot.create('error', error.msg, '');

                            }
                        );
                },
                onCancel() {
                }
            });

        } else if (event.names.key === 'marigeList') {

            this.utilityService.getData(appConfig.testUrl  + '/deliveries/' + event.guid + '/mergeReport')
                .subscribe(
                    (val) => {
                      
                        this.isTestModal = true;
                        this.errorInfo =  val.result;
                        this.revertListData = val.result.conflictReportDetails; // 显示弹窗
                       for(let i=0;i<this.revertListData.length;i++){
                           if(this.revertListData[i].whoChangedConflictDetails.length>0){
                               this.revertListData[i].buttonData = [
                                   {key: 'WhoChange', value: '查看谁也再改' }
                               ]
                           }
                    
                       }
          
                        console.log(this.revertListData)
                        this.nznot.create('success', val.msg, val.msg);
                    }   ,
                    (error) => {
                        this.nznot.create('error', error.msg, '');
                    }
                );
        } else if (event.names.key === 'detail') {
            this.utilityService.getData( appConfig.testUrl + appConfig.API.deliveries + '/' + event.guid + '/deliveryLists'  )
                .subscribe(
                    (val) => {
                        if (val.code === '200') {
                            this.mergeListDetail = val.result;
                            this.mergeVisible = true;
                        }
                    }, (error) => {
                       this.nznot.create('error', error.code,  error.msg);
                    });


        }else if(event.names.key === 'upd') {

            this.guidDelivery = event.guid;
            this.utilityService.getData( appConfig.testUrl + '/deliveries/' + event.guid + '/profileDateilVerify' )
                .subscribe(
                    (val) => {

                        if(val.code === '200') {
                            this.updPackTiming  = val.result

                            this.updPackTiming['unixTime'] = moment(this.updPackTiming['deliveryTime']).format('YYYY-MM-DD 00:00:00.000')
                            localStorage.setItem('time',this.updPackTiming['unixTime']);
                            for (let i = 0; i < this.updPackTiming['packTimeDetails'].length; i++) {
                                if (this.updPackTiming['packTimeDetails'][i]['isOptions'] === 'D'){
                                    //  this.updPackTiming['packTimeDetails'][i]['check'] = true
                                    this.updPackTiming['packTiming']   =   this.updPackTiming['packTimeDetails'][i]['packTime']
                                }else {
                                    //  this.updPackTiming['packTimeDetails'][i]['check'] = false
                                }
                            }
                            this.packTime = this.updPackTiming['packTiming']
                            this.updPackTiming['deliveryTime'] = new Date(this.updPackTiming['deliveryTime']);
                            this.updEnvironment = true;
                        }

                    }, (error) => {
                       this.nznot.create('error', error.code,  error.msg);
                    });
        } else if (event.names.key === 'copy') {
            this.copyseniorGuid = event.guid;
            this.getcheckOptionOne(event.workGuid);
            this.copyVisible = true;
        }else if(event.names.key==='refuse'){
             let self = this;
             this.confirmServ.confirm({
                title  : '提示',
                 content: '<b>您是否确认要打回工作项[ '+event.itemName+' ]的申请!</b>',
                showConfirmLoading: true,
                onOk() {
                    self.utilityService.getData(appConfig.testUrl  + '/deliveries/'+event.guid+'/dislodgeDelivery')
                        .subscribe(
                            (val) => {
                                 self.getData()
                                self.nznot.create('success', val.msg, val.msg);

                            }   ,
                            (error) => {
                                self.nznot.create('error', error.msg, '');

                            }
                        );
                },
                onCancel() {
                }
            });
        }else if(event.names.key === 'test' ){
             this.utilityService.getData(appConfig.testUrl  + '/deliveries/' + event.guid + '/forecast')
                        .subscribe(
                            (val) => {
                                if (val.result === '') { // 合并无异常，不弹框
                                    this.isTestModal = false;
                                    this.nznot.create('success', val.msg, val.msg);
                                } else {
                                    this.isTestModal = true;
                                    this.errorInfo =  val.result;
                                    this.revertListData = val.result.conflictReportDetails; // 显示弹窗
                                  for(let i=0;i<this.revertListData.length;i++){
                           if(this.revertListData[i].whoChangedConflictDetails.length>0){
                               this.revertListData[i].buttonData = [
                                   {key: 'WhoChange', value: '查看谁也再改' }
                               ]
                           }}
                                    this.nznot.create('error', val.msg, val.msg);
                                }

                            }   ,
                            (error) => {
                                this.nznot.create('error', error.msg, '');

                            }
                        );
        }
    }
    iswhochangeModal =false;
    isTestModal = false;
    _disabledDate(current: Date): boolean {
        let time = localStorage.getItem('time');
        return current && current.getTime() < new Date(time).getTime();
    }

    onChange(updPackTiming) {

        if (updPackTiming.deliveryTime.getTime() !== new Date(updPackTiming.unixTime).getTime()) {
            for (let i = 0; i < updPackTiming.packTimeDetails.length; i++) {
                if (updPackTiming.packTimeDetails[i].isOptions === 'N') {
                    updPackTiming.packTimeDetails[i].isOptions = 'No';
                }
            }
        } else {
            this.updPackTiming['packTiming'] = this.packTime
            for (let i = 0; i < updPackTiming.packTimeDetails.length; i++) {
                if (updPackTiming.packTimeDetails[i].isOptions === 'No') {
                    updPackTiming.packTimeDetails[i].isOptions = 'N';
                }
            }

        }
    }
    submitUpd() {
        let obj = {
            guidDelivery: this.guidDelivery,
            deliveryTime:moment(this.updPackTiming['deliveryTime'].getTime()).format('YYYY-MM-DD'),
            packTiming: this.updPackTiming['packTiming']
        }

        this.utilityService.putData( appConfig.testUrl + '/deliveries/deliveryTimePackTime', obj,  )
            .subscribe(
                (val) => {
                    this.updEnvironment = false;
                    this.getData();
                    if (val.code === '200') {
                        this.nznot.create('success', val.msg, '');
                    }

                },(error) => {
                   this.nznot.create('error', error.code,  error.msg);

                });
    }

    // 列表按钮方法
    buttonDataHandler(event) {



    }


 
    buttonmerge(event) {
      console.log(event)
      if(event.names.key === 'WhoChange' ){
            // 
            this.iswhochangeModal = true;
            for(let i = 0;i<event.whoChangedConflictDetails.length;i++){
                event.whoChangedConflictDetails[i].workitem = event.whoChangedConflictDetails[i].guidWorkitem.target;
            }
            this.childListData = event.whoChangedConflictDetails
            
     console.log(event)
        }
    }



    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {
        if (event) {
            this.isNext = false;
        }

    }

     allChecked = false;
  indeterminate = false;
  //导出清单全选
  updateAllChecked(){
     this.indeterminate = false;

    if (this.allChecked) {
      this.appendSelect.forEach(item => {
          item.check = true
      });
    } else {
      this.appendSelect.forEach(item => {
          item.check = false
        });
    }

  }
    //取消清单全选
  updateSingleChecked(){
     if (this.appendSelect.every(item => item.check === false)) {
      this.allChecked = false;
      this.indeterminate = false;
    } else if (this.appendSelect.every(item => item.check === true)) {
      this.allChecked = true;
      this.indeterminate = false;
    } else {
      this.indeterminate = true;
    }
    console.log(this.indeterminate)
    console.log(this.appendSelect)

  }
    selectedRow(event) { // 选中方法，折叠层按钮显示方法

    }
    isSpinning=false;
     two = [];
    isNow= false;
        selected(event){

             let star = '';
                let end = '';
              let index = '';
              let indexs = '';
            this.isSpinning = true;
              this.utilityService.getData( appConfig.testUrl + '/checks/' +this.checkId+'/checkList/delivery/'+event.value+'/list' )
                         .subscribe(
                         (val) => {
                              this.isWorkitem.title=event.label
                             this.isNow = true
                             this.isSpinning = false;
                             this.checkModalData = val.result;
                             console.log(this.checkModalData)
                                for  (let i = 0; i < this.checkModalData.length; i ++) {
                                for (let x = 0; x < this.checkModalData[i].deliveryPatchDetails.length; x ++) {

                                    for (let  y = 0; y < this.checkModalData[i].deliveryPatchDetails[x].fileList.length; y ++) {
                                          switch(this.checkModalData[i].deliveryPatchDetails[x].fileList[y].confirmStatus){
                                                case '已合并':
                                                this.checkModalData[i].deliveryPatchDetails[x].fileList[y]['buttons'] = true;
                                                break;
                                                 case '不投放':
                                                this.checkModalData[i].deliveryPatchDetails[x].fileList[y]['buttons'] = false;
                                                break;
                                                default:
                                                this.checkModalData[i].deliveryPatchDetails[x].fileList[y]['buttons'] = null
                                         }
                                        if (this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath) {
                                            index = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.indexOf(this.checkModalData[i].projectName);
                                            this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substring(index, this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length);
                                              if(this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length > 40) {
                                                        star = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substr(0, 15)
                                                        end = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.substr(this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath.length - 15)
                                                            this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPathstr = star + '...' + end;
                                                        }else{
                                                           this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPathstr = this.checkModalData[i].deliveryPatchDetails[x].fileList[y].fullPath;
                                                     }

                              }
                                    }
                                }
                            }

                            //  this.deliveryPatchDetails.fileList= val.result.records;
                            //   this.deliveryPatchDetails.projectName=val.result.records[0].partOfProject
                            //     this.deliveryPatchDetails.patchType=val.result.records[0].patchType
                            //       this.deliveryPatchDetails.deployWhere=val.result.records[0].deployWhere
                        console.log( val)
                 })

        }

    // 搜索框
    // search() {

    // }


    // 比对界面
    checkVisible = false; // 默认是关闭的



    // 核对清单弹框
    environment = [
        { text: 'SIT', value: false, key: 'SIT' },
        { text: 'SIT Dev', value: false, key: 'Dev' },
        { text: 'UAT', value: false, key: 'UAT' }
    ]


    save() {
        this.modalVisible = false; // 关闭选择框
        this.checkVisible = true; // 打开核对弹出框
    }



    savemergeisInfo () {
        this.profiles = [];
        for (let i = 0; i < this.elementScice.length; i ++) {
            if (this.elementScice[i].check && this.elementScice[i].times) {
                let obj = {
                    guidProfiles: this.elementScice[i].guid,
                    packTiming: this.elementScice[i].times,
                    name: this.elementScice[i].manager,
                    profilesName: this.elementScice[i].profilesName
                };
                this.profiles.push(obj);
            }
        }

        if ( this.profiles.length === 0) {
            this.nznot.create('error', '请选择投放环境', '请选择投放环境');
            return;
        }

        this.deliveryTime = moment(this.deliveryTime).format('YYYY-MM-DD');
        const obj = {
            mergeList: this.mergeList,
            deliveryTime:  this.deliveryTime,
            profiles:  this.profiles

        };

        this.mergeisVisible = false;
        let index = 0;
        this.utilityService.postData(appConfig.testUrl  + appConfig.API.mergeInfo, obj)
            .subscribe(
                (val) => {
                    if (val.code === '200') {


                    }else {
                        this.nznot.create('error', val.msg, val.msg);
                    }

                },
                (error) => {
                   this.nznot.create('error', error.code,  error.msg);
                }


            )
        ;

    }



    // 状态
    returns(item , S ) {

        if ( S === 0) {
            this.guid = item;

            this.istextVisible = true;

        }else {
            let  url = appConfig.testUrl + '/checks/delivery/' + item + '/result';
            if (S === 1) {
                S = 'S';
            }else {
                S = 'F';
            }
            const obj = {
                result : S,
                desc : this.inputValue
            };

            this.utilityService.postData( url, obj)
                .subscribe(
                    (val) => {
                        if (val.code === '200') {
                            this.istextVisible = false;
                            this.checkListVisible = false;
                            this.nznot.create('success', val.msg, val.msg);
                        }else {
                            this.nznot.create('error', val.msg,  '');
                        }
                    },
                    (error) => {
                      this.nznot.create('error', error.code,  error.msg);
                    }
                );
        }


    }

    sussess(item , status) {

    }

    errors() {

    }


    // 关闭核对清单
    // checkSave() {
    //     this.checkVisible = false;
    //     this.mergeVisible = false;
    //     this.utilityService.postData(appConfig.testUrl  + appConfig.API.merge, this.profilesData, { Authorization: this.token})
    //         .subscribe(
    //             (val) => {

    //                 if (val.code === '200') {

    //                     this.nznot.create('success', val.msg, val.msg);
    //                 }else {
    //                     this.nznot.create('error', val.msg, val.msg);
    //                 }

    //             },
    //             (error) => {
    //                this.nznot.create('error', error.code,  error.msg);
    //             }
    //         );
    // }



    // 子组件核对详情的方法
  checkboxChild(event){
        let s=event.s;
        let i = event.i;
        let status =event.status
        s.checked = status
      let arr = []

      i.children.forEach(index=>{

             if(s.name === index.name){
                  index = s
             }
             if(index.checked){
                   arr.push(index)
             }

      })
      let obj=true;
      if(arr.length === 1){
            obj = true
        }else{
            obj = false
         }

      i.children.forEach(index=>{
           if( this.isPro ==='S'){
                  if(index.name === s.name){
                        index.checked = status
                 }else{
                      index.checked = false
                 }
              }
          if(index.checked){
                  index.disabled = obj
          }

            })
    }
      checkboxPatch(event){
        let i = event.i;
        let status =event.status
            i.checked = status

            this.addPatchType.forEach(x=>{
            if( this.isPro ==='S'|| this.isPro ==='I'){
                 if(i.exportType !== x.exportType){
                       x.checked = false

                 }
                }
                //  if(!x.checked){
                     x.children.forEach((j,index)=>{
                           j.checked = x.checked

                         if( this.isPro ==='S' && x.checked ){
                             console.log(index)
                                if( index === 0){
                                        j.checked = x.checked
                                }else{
                                    j.checked  = false
                                }
                         }


                       if( i.children.length===1){
                            j.disabled = true
                        }else{
                            if(this.isPro ==='P'||this.isPro ==='C'){
                                 j.disabled = true
                            }else{
                                  j.disabled = false
                            }
                        }


              })
                // }


            })


         console.log(i)
  }


    iStouchan = false;
    idcheck: any
    mergeId: any;
    mergeguid: any
    guidprent = [{guid: '', guidWorkitem: ''}];
    mergeData: any[] = []; // 核查有异议的数据
    addPatchType = [];
    isPro:string;
    buttonClick(event) {
        let type = event.index;
        // let id = event.id.guid;
        let soyin = event.soyin;
        this.Deliveryguid = event.id.guidDelivery
        this.guidCheckList = event.id.guid;
        if ( type === 4) {
            // this.iStouchan = true;
             this.addPatchType = []
                this.utilityService.getData( appConfig.testUrl + '/checks/'+ this.guidCheckList+'/pathPatchDeploy')
                         .subscribe(
                         (val) => {
                           console.log(val)
                             val.result.patchDeploy = JSON.parse(val.result.patchDeploy)
                             val.result.patchDeploy.forEach(i=>{
                                 i.deployType = i.deployType.split(',')
                             })
                             let childDisabl;
                             let disabl;
                             let check = true;

                             this.isPro = val.result.projectType
                              switch(val.result.projectType){
                                      case 'S':
                                      childDisabl = false
                                      disabl = false;
                                      break;
                                      case 'I':
                                      childDisabl = false
                                      disabl = false;
                                      break;
                                       case 'C':
                                      childDisabl = true
                                      disabl = false;
                                      break;
                                      default:
                                      childDisabl = true
                                      disabl = true;
                                      break;
                                    }

                                    if(val.result.patchDeploy.length === 1){
                                        disabl = true
                                    }

                                   val.result.patchDeploy.forEach((j,index)=>{
                                          j.checked = check;
                                        if(val.result.projectType === 'I' || val.result.projectType === 'S'){
                                            if(index===0){
                                                  j.checked = check;
                                            }else{
                                                  j.checked = false;
                                            }

                                        }

                                        j.disabled = disabl;
                                        j.children = []

                                     if(j.deployType.length ===1){
                                              childDisabl = true
                                     }
                                     let flage = true;
                                        j.deployType.forEach((l,it)=>{
                                            if(j.checked){
                                                if(val.result.projectType === 'S' ){
                                                    if(it === 0 ){
                                                     flage = true
                                                }else{
                                                      flage = false
                                                }
                                                }
                                                   j.children.push({name:l,checked:flage,disabled:childDisabl})
                                            }else{
                                                  j.children.push({name:l,checked:false,disabled:childDisabl})
                                            }


                                        })
                                    })
                                     this.addPatchType =  val.result.patchDeploy
                                       this.iStouchan = true;
                                   console.log( this.addPatchType )
                         },(error)=>{
                               this.nznot.create('error', error.code,  error.msg);
                         })


        }else {
            this.utilityService.putData( appConfig.testUrl + '/checkLists/' +  this.guidCheckList + '/status/' + type, {},  )
                .subscribe(
                    (val) => {

                        if (val.code === '200') {

                            this.nznot.create('success', val.msg,  '');
                             if (type === 3) {
                             event.id.confirmStatus='删除'
                             event.id.checkbuttons = false;
                           }
                        }
                    }, (error) => {
                       this.nznot.create('error', error.code,  error.msg);
                    });

        }
    }

    titlestr = '完成此项操作吗？'
    status:string;
    mergeClick(index) {
           this.isOkLoading = false;
        switch (index) {
            case 0 :
                this.mergeisVisible = false
                return;
            case 1 :
                this.status  = 'F'
                this.titlestr = '作废核对'
                this.isVisibleSuccess = true
                break;
            case 2 :

                this.status = 'S'
                this.titlestr = '完成核对'
                this.isVisibleSuccess = true
                break;
        }
        console.log(this.titlestr)
    }
    isVisibleSuccess = false;
    isOkLoading = false;
    handleOk(){
     this. isOkLoading = true;

     this.utilityService.putData( appConfig.testUrl + '/checks/' + this.checkId + '/status/' + this.status, {},  )
                    .subscribe(
                        (val) => {

                            if (val.code === '200') {
                                this.nznot.create('success', val.msg,  '');
                                this.isVisibleSuccess =false;
                                this. isOkLoading = false;
                                this.getData()
                                this.mergeisVisible = false;

                            }else {
                                this.nznot.create('error', val.msg, '');
                            }
                        },(error) => {
                             this. isOkLoading = false;
                            this.isVisibleSuccess =false;
                           this.nznot.create('error', error.code,  error.msg);
                        })
    }

    buttonEvent(event) {
        this.mergeId = event.guid;
        if (event.names.key === 'merge') {
            this.idcheck = event.guid;
            let index = '';
            let indexs = '';
            this.detailVisible = true;
            this.utilityService.getData( appConfig.testUrl + '/checks/' + event.guid)
                .subscribe(
                    (val) => {
                        this.detailVisible = true;
                        this.checkModalData = val.result.deliveryDetails;
                        this.mergeData  = val.result.mergeLists;
                        for  (let i = 0; i < this.mergeData.length; i ++) {
                            if (this.mergeData[i].fullPath) {
                                indexs = this.mergeData[i].fullPath.indexOf(this.mergeData[i].partOfProject);
                                this.mergeData[i].fullPath = this.mergeData[i].fullPath.substring(indexs, this.mergeData[i].fullPath.length);
                            }
                        }
                        for  (let i = 0; i < this.checkModalData.length; i ++) {
                            this.guidprent[i]['guid'] = this.checkModalData[i].delivery.guid;
                            this.guidprent[i]['guidWorkitem'] = this.checkModalData[i].delivery.guidWorkitem.target
                            for (let j = 0; j < this.checkModalData[i].detailList.length; j ++) {
                                for (let x = 0; x < this.checkModalData[i].detailList[j].deliveryPatchDetails.length; x ++) {
                                    for (let  y = 0; y < this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList.length; y ++) {
                                        this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y]['buttons'] = null
                                        if (this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y].fullPath) {
                                            index = this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y].fullPath.indexOf(this.checkModalData[i].detailList[j].projectName);
                                            this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y].fullPath = this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y].fullPath.substring(index, this.checkModalData[i].detailList[j].deliveryPatchDetails[x].fileList[y].fullPath.length);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    , (error) => {
                       this.nznot.create('error', error.code,  error.msg);
                    })


        } else if (event.names.key  === 'details') {

        } else if (event.names === '成功') {
            alert('成功的方法');
        }



    }

    returnsclick(event) {
        let item = event.index
        this.mergeguid = event.it.value
        let S = '';
        let  url = appConfig.testUrl + '/checks/delivery/' + this.mergeguid + '/result';
        if (item === 1) {
            S = 'S';
        }else {
            S = 'F';
        }
        const obj = {
            result : S,
            desc : this.inputValue
        };


        this.utilityService.putData( url, obj,  )
            .subscribe(
                (val) => {
                    if (val.code === '200') {
                        this.istextVisible = false;
                          if (item === 1 ) {
                           event.it.disabledS = true
                           event.it.deliveryResult = '核对成功';
                           event.it.color = '#8310e9'
                             event.it.icon = 'anticon-exclamation-circle-o'
                       }else  if (item === 2 ){
                           event.it.disabledS = true

                           event.it.deliveryResult = '申请失败';
                           event.it.color = '#f04134'
                           event.it.icon = 'anticon-close-circle-o'
                       }
                        this.deliveryResults.forEach(i=>{
                            if( event.it.deliveryResult == i.value){
                            event.it.color = i.color
                            event.it.icon = i.icon
                            }
                        })
                        this.nznot.create('success', val.msg, val.msg);
                    }else {
                        this.nznot.create('error', val.msg, '');
                    }

                }
                ,
                (error) => {

                   this.nznot.create('error', error.code,  error.msg);
                }
            );
        //    }


    }
guidCheckList:string;
Deliveryguid:string;
    checkSavemerge(event) {

        let objs = event.arr


          let obj ={
            guidDelivery: event.guidDelivery,//工作项ID
            deployConfig: event.arr,
            guidCheckList:this.guidCheckList,// 核对代码的guid
        }
        console.log(objs)

        this.utilityService.putData( appConfig.testUrl + '/checkLists/' + event.errorId + '/delivery', obj,  )
            .subscribe(
                (val) => {
                    if (val.code === '200') {

                        this.mergeListData.forEach((result, i) => {
                            if (result.guid === event.errorId) {
                                this.mergeListData[i].checkbuttons = true;
                                this.mergeListData[i].confirmStatus = '加入投放';
                                  this.mergeListData[i].patchType = event.patchType
                            }
                        })
                        this.nznot.create('success', val.msg,  '');
                        this.iStouchan = false;
                    }
                }, (error) => {
                   this.nznot.create('error', error.code,  error.msg);
                });
    }


    buttonDatas(event){

    }

    // 打印界面
    isVisible = false; // 默认关闭
    workItem = false;
    mergeisVisible = false;
    checkOptionsOne  = [
        { label: 'TWS改版', value: 'TWS' },
        { label: '1618 国际结算', value: '1618' },
        { label: '无纸化',  value: 'wu' }
    ];
    text = [
        {label: 'SIT Dev - 开发集成测试', value: 'TWS',
            time:
                [
                    {times: 'A', key: 'cd'},
                    {times: 'B',  key: 'cd'},
                    {times: 'C',  key: 'cd'}]},
        {label: '国际结算', value: '1618', time:[{times: '13:00', key: 'cs'},{times: '15:00', key: 'cs'},{times: '17:00', key: 'cs'}]},
        {label: '无纸化', value: 'wu', time:[{times: '11:00', key: 'dw'},{times: '16:00', key: 'dw'}, {times: '20:00', key: 'dw'}]},
    ];



    // 合并的确定
    determine() {
        this.mergeVisible = false;
    }
    moreclick() {
    }


    importCurrent = 0;
    // 导出接口
    // 查询导出环境和窗口
    isClick() {

    }
    // 上一步
    importpre() {
        this.importCurrent -= 1;
        this.changeContent();
    }
    // 下一步
    isExport =  true;
    appendSelect: any;
    nextImport() {
        // 下一步验证
        let profiles = '';
        let packTiming = '';
        for (let i = 0; i < this.elementScice.length; i ++) {
            if (this.elementScice[i].check && this.elementScice[i].times) {
                profiles = this.elementScice[i].guid;
                packTiming = this.elementScice[i].times;
            }
        }
        let exportObj = {
            deliveryTime: moment(this.deliveryTime).format('YYYY-MM-DD'),
            packTiming: packTiming,
            guidProfile: profiles
        };

        let url = '';
        if (profiles !== '' && packTiming !== '') {
            url = appConfig.testUrl + appConfig.API.export;
        }else {
            this.nznot.create('error', '请选择运行环境！', '请选择运行环境！');
            return;
        }
        if (this.importCurrent === 0) {
            // 测试接口,先测试
            this.utilityService.postData( url, exportObj)
                .subscribe(
                    (val) => {
                        val.result.forEach(i=>{
                            i.check = false
                        })
                        this.appendSelect = val.result;

                        this.importCurrent += 1;
                        this.changeContent();
                    },
                    (error) => {
                        this.importCurrent = 0;
                      this.nznot.create('error', error.code,  error.msg);
                    }
                );
        }

    }

    // 保存
    loadingdaochu = false;
    //下载
    importSave(){

          let workGuid = [];
        // 多选导出
        _.forEach(this.appendSelect , function (select) {
            if (select.check) {
                workGuid.push(select.guid);
            }
        })
        if ( workGuid.length !== 0) {
              this.nznot.create('info',' 正在下载，请等待' , '');
      this.utilityService.downpostData(appConfig.testUrl + appConfig.API.excel +  'excel',workGuid,  )
            .subscribe(
            (val) => {
           const file = new Blob([val._body], {type: "application/vnd.ms-excel"});
            const a = document.createElement('a');
            a.id = 'tempId';
            document.body.appendChild(a);
            a.download = '标准清单.xls';
            a.href = URL.createObjectURL(file);
            a.click();
            this.isVisible = false;
            const tempA = document.getElementById('tempId');
            if (tempA) {
            tempA.parentNode.removeChild(tempA);

             this.nznot.create('success',' 下载成功' , '');
            }
             },(error)=>{

                this.nznot.create('error', error.code , error.msg);
           })

      }else{
          this.nznot.create('error',' 请勾选工作项' , '');
      }
    }



    // 拷贝投放
    copySave() {
        this.profiles = [];
        for (let i = 0; i < this.elementCopy.length; i ++) {

            if (this.elementCopy[i].check && this.elementCopy[i].times) {
                let obj = {
                    guidProfiles: this.elementCopy[i].guidProfile,
                    profilesName: this.elementCopy[i].profilesName,
                    packTiming: this.elementCopy[i].times,
                    applyAlias: this.elementCopy[i].deliveryName,
                    deliveryTime: moment(this.elementCopy[i].deliveryTime).format('YYYY-MM-DD')
                };
                this.profiles.push(obj);
            }
        }
        let splicingObj = {
            guidDelivery : this.copyseniorGuid, // 清单的guid
            profiles: this.profiles,
        };

        this.utilityService.postData(appConfig.testUrl  + appConfig.API.newProfiles, splicingObj)
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.code , val.msg);
                    this.copyVisible = false;
                    this.launchVisible = true;
                    this.copyTitle = '投放新环境成功';
                    this.detailInfo = val.result; // 返回的数据有问题
                },
                (error) => {
                   this.nznot.create('error', error.code,  error.msg);
                    // this.getData();
                }
            );

    }

    // 比较copy的时间
    oncopyChange(time, array) {
        if (time.getTime() !== new Date(array.unixTime).getTime()) {
            for (let i = 0; i < array.packTimeDetails.length; i++) {
                if (array.packTimeDetails[i].isOptions === 'N') {
                    array.packTimeDetails[i].isOptions = 'No';
                }
            }
        } else {
            for (let i = 0; i < array.packTimeDetails.length; i++) {
                if (array.packTimeDetails[i].isOptions === 'No') {
                    array.packTimeDetails[i].isOptions = 'N';
                }
            }
        }

        if (time.getTime() < new Date(array.unixTime).getTime()) {
            array.deliveryTime = new Date(array.unixTime).getTime();
            this.nznot.create('error', '选择时间不能小于初始时间' , '');
        } else {
        }
    }

    copyCancel() {
        this.getData(); // 重新查询
        console.log('s');
        this.launchVisible = false;
    }




}
